package com.zjd.association.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjd.association.common.PageWrapper;
import com.zjd.association.entity.*;
import com.zjd.association.mapper.*;
import com.zjd.association.service.MembersService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 成员信息 服务层实现类
 *
 * @author: zjd
 * @date: 2024-5-22
 */
@Service
@RequiredArgsConstructor
public class MembersServiceImpl implements MembersService {

    private final TeamsMapper teamsMapper;

    private final MembersMapper membersMapper;

    private final PayLogsMapper payLogsMapper;

    private final ActiveLogsMapper activeLogsMapper;

    private final ApplyLogsMapper applyLogsMapper;

    @Override
    public void add(Members members) {
        membersMapper.insert(members);
    }

    @Override
    public void update(Members members) {
        membersMapper.updateById(members);
    }

    @Override
    public void delete(Members members) {
        QueryWrapper<PayLogs> qw_pay = new QueryWrapper<>();
        qw_pay.eq("user_id", members.getUserId());
        payLogsMapper.delete(qw_pay);

        QueryWrapper<ActiveLogs> qw_active = new QueryWrapper<>();
        qw_active.eq("user_id", members.getUserId());
        activeLogsMapper.delete(qw_active);

        QueryWrapper<ApplyLogs> qw_apply = new QueryWrapper<>();
        qw_apply.eq("user_id", members.getUserId());
        applyLogsMapper.delete(qw_apply);
        membersMapper.deleteById(members);

        Teams team = teamsMapper.selectById(members.getTeamId());
        team.setTotal(team.getTotal() - 1);
        teamsMapper.updateById(team);
    }

    @Override
    public Members getOne(String id) {
        return membersMapper.selectById(id);
    }

    /**
     * 指定用户是否是管理员
     *
     * @param userId 指定用户ID
     * @param teamId 团队ID
     * @return 结果
     */
    @Override
    public Boolean isManager(String teamId, String userId) {
        QueryWrapper<Teams> qw = new QueryWrapper<>();
        qw.eq("manager", userId);
        qw.eq("id", teamId);
        return teamsMapper.selectCount(qw) > 0;
    }

    /**
     * 分页查询成员信息
     *
     * @param pageIndex 当前页码
     * @param pageSize  每页数据量
     * @param teamName  活动名称
     * @param userName  成员姓名
     * @return 成员信息
     */
    @Override
    public PageWrapper getPageAll(Long pageIndex, Long pageSize, String teamName, String userName) {
        Page<Map<String, Object>> page
                = membersMapper.qryPageAll(new Page<>(pageIndex, pageSize), teamName, userName);
        return PageWrapper.parsePage(page);
    }

    /**
     * 分页查询成员信息
     *
     * @param pageIndex 当前页码
     * @param pageSize  每页数据量
     * @param manId     管理员ID
     * @param teamName  活动名称
     * @param userName  成员姓名
     * @return 成员信息
     */
    @Override
    public PageWrapper getPageByManId(Long pageIndex, Long pageSize, String manId, String teamName, String userName) {
        Page<Map<String, Object>> page
                = membersMapper.qryPageByManId(new Page<>(pageIndex, pageSize), manId, teamName, userName);
        return PageWrapper.parsePage(page);
    }

}